package authenum

import (
	"yunj/pkg/yunj/core/enum"
)

// 定义常量
const (
	// 常规
	PAGE_TYPE_NORMAL string = "normal"

	// 侧边栏菜单
	PAGE_TYPE_SIDEBAR_MENU string = "sidebar_menu"

	// 顶部菜单
	PAGE_TYPE_TOP_MENU string = "top_menu"
)

// 实现EnumInerface接口
type PageType struct {
	enum.Enum[string]
}

func (e *PageType) AllConstAttrs() map[string]map[string]any {
	return map[string]map[string]any{
		PAGE_TYPE_NORMAL: {
			"desc":        "常规",
			"dbTypeValue": TYPE_NORMAL,
		},
		PAGE_TYPE_SIDEBAR_MENU: {
			"desc":        "侧边栏菜单",
			"dbTypeValue": TYPE_SIDEBAR_MENU,
		},
		PAGE_TYPE_TOP_MENU: {
			"desc":        "顶部菜单",
			"dbTypeValue": TYPE_TOP_MENU,
		},
	}
}

// 是否为菜单
func (e *PageType) IsMenu() (res bool) {
	if e.Value == PAGE_TYPE_SIDEBAR_MENU || e.Value == PAGE_TYPE_TOP_MENU {
		res = true
	}
	return
}

// 获取数据库类型值
func (e *PageType) GetDbTypeValue() (res any) {
	res = e.GetAttr("dbTypeValue")
	return
}

// 获取数据库类型uint8值
func (e *PageType) GetDbTypeUint8Value() (res uint8) {
	resRaw := e.GetAttr("dbTypeValue")
	res, _ = enum.ToUint8(resRaw)
	return
}

// instanceCache 存储已经创建的实例
var pageTypeInstanceCache = make(map[string]*PageType)

// 根据value获取枚举
func PageTypeByValue(value string) *PageType {
	if instance, exists := pageTypeInstanceCache[value]; exists {
		return instance
	}
	e := &PageType{}
	enum.SetEnumInstance(&e.Enum, e, value)
	pageTypeInstanceCache[value] = e
	return e
}

var PageTypeEnum *PageType = func() *PageType {
	e := &PageType{}
	enum.SetEnumInstance(&e.Enum, e)
	return e
}()
